﻿Imports System.Data.SqlClient
Imports System.IO

Partial Class uploadimg
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MIMEType As String = Nothing
        Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower()

        Select Case extension
            Case ".gif"
                MIMEType = "image/gif"
            Case ".jpg", ".jpeg", ".jpe"
                MIMEType = "image/jpeg"
            Case ".png"
                MIMEType = "image/png"

            Case Else
                'Invalid file type uploaded
                Exit Sub
        End Select


        Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Management_Info_SysConnectionString").ConnectionString)

            Const SQL As String = "INSERT INTO [img_items] ([img_table], [img_desc], [img]) VALUES (@img_table, @img_desc, @img)"
            Dim myCommand As New SqlCommand(SQL, myConnection)
            'myCommand.Parameters.AddWithValue("@img_desc", FileUpload1.FileName.Trim)
            myCommand.Parameters.AddWithValue("@img_table", "Default")
            myCommand.Parameters.AddWithValue("@img_desc", MIMEType)

            'Load FileUpload's InputStream into Byte array
            Dim imageBytes(FileUpload1.PostedFile.InputStream.Length) As Byte
            FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
            myCommand.Parameters.AddWithValue("@img", imageBytes)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()
        End Using
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Image1.ImageUrl = "http://localhost:23590/imggen.aspx?PictureID=ita_items114515165-15195-65165"


    End Sub
End Class
